const { ccclass, property } = cc._decorator;

@ccclass
export default class SpineAttacheUtil {
private eBoneName: string = "kuang";

public addNodeToBone(t: any, a: any, o: string = this.eBoneName) {
    if (!t || !a || !o) return null;

    var i = a.attachUtil;
    if (!i) return null;

    var r = i.generateAttachedNodes(o);
    if (!r || r.length == 0) return null;

    r[0].addChild(t);
    return t;
};

public generateAllByDefaultMode(e: any, t: any, a: any) {
    if (!e || !t) return null;

    if (!t.attachUtil) return null;

    var o = [];

    var i = this.getAllBoneNameByDefaultMode(t);

    for (var r = 0; r < i.length; r++) {
        var n = i[r];
        var l = this.addAllPolygonNodeToSkeleton(e, t, n, a);
        o = o.concat(l);
    }

    return o;
};

public generateAllByBonePrefix(e: any, t: any, a: any, o: string = "kuang") {
    if (!e || !t) return null;

    if (!t.attachUtil) return null;

    var i = [];

    var r = this.getAllBoneNameByDefaultMode(t, o);

    for (var n = 0; n < r.length; n++) {
        var l = r[n];
        var s = this.addAllPolygonNodeToSkeleton(e, t, l, a);
        i = i.concat(s);
    }

    return i;
};

public addAllPolygonNodeToSkeleton(t: any, a: any, o: string = this.eBoneName, i: any) {
    if (!t || !a || !o) return null;

    var r = a.attachUtil;
    if (!r) return null;

    var n = r.generateAttachedNodes(o);
    if (!n || n.length == 0) return null;

    var l = n[0];

    var s = this.getAllSlotNameByBoneName(a, o);

    var c = [];

    for (var u = 0; u < s.length; u++) {
        var p = a.findSlot(s[u]);
        if (p) {
            var d = p.attachment;
            if (d) {
                var h = d.vertices;
                if (h) {
                    var f = this.generatePolygonCollider(h, i);
                    if (f) {
                        f.node.x = f.node.y = 0;
                        f.node.group = t;
                        f.node.name = o;
                        l.addChild(f.node);
                        c.push(f);
                    }
                }
            }
        }
    }

    return c;
};

public addPolygonNodeToSkeleton(t: any, a: any, o: string = this.eBoneName, i: any, r: any) {
    if (!(t && a && o && i)) return null;

    var n = a.attachUtil;
    if (!n) return null;

    var l = n.generateAttachedNodes(o);
    if (!l || l.length == 0) return null;

    var s = l[0];
    var c = a.findSlot(r);
    if (!c) return null;

    var u = c.attachment.vertices;
    var p = this.generatePolygonCollider(u, i);
    if (!p) return null;

    p.node.x = p.node.y = 0;
    p.node.group = t;
    p.node.name = o;

    s.addChild(p.node);

    return p;
};

public getAllSlotNameByBoneName(t: any, a: string = this.eBoneName) {
    if (!t || !a) return null;

    if (!t.skeletonData) return null;

    var o = t.skeletonData.skeletonJson.slots;

    var i = [];

    for (var r = 0; r < o.length; r++) {
        var n = o[r];
        if (n.bone == a) {
            i.push(n.name);
        }
    }

    return i;
};

public getAllBoneNameByDefaultMode(e: any, t: string = "kuang") {
    if (!e) return null;

    if (!e.skeletonData) return null;

    var a = e.skeletonData.skeletonJson.bones;

    var o = [];

    for (var i = 0; i < a.length; i++) {
        var r = a[i];
        if (r.name.indexOf(t) != -1) {
            o.push(r.name);
        }
    }

    return o;
};

public generatePolygonCollider(e: any, t: any) {
    if (!e || e.length <= 0) return null;

    var a = t ? cc.instantiate(t) : new cc.Node();
    a.active = true;

    var o = a.addComponent(cc.PolygonCollider);
    o.points = [];

    for (var i = 0; i < e.length; i++) {
        o.points.push(cc.v2(e[i], e[i + 1]));
        i++;
    }

    return o;
};

public getAttachedNodes(e: any, t: any) {
    if (!e || !t) return null;

    var a = e.attachUtil;
    return a ? a.getAttachedNodes(t) : null;
};

public destroyAttachedNodes(e: any, t: any) {
    if (!e || !t) return null;

    var a = e.attachUtil;
    a && a.destroyAttachedNodes(t);
};

public destroyAllAttachedNodes(e: any) {
    if (!e) return null;

    var t = e.attachUtil;
    t && t.destroyAllAttachedNodes();
};
}